import { PORT1 } from "@/api/config/servicePort";
import http from "@/api";

export namespace Route {
  export interface RouteListItem {
    id: string;
    routeCode: string;
    routeName: string;
    status: string;
    routeType: string;
    version: string;
    description: string;
    createdBy: string;
    createdTime: string;
    updatedBy: string;
    updatedTime: string;
    deleted: number;
  }

  export interface CreateRouteReq {
    routeCode: string;
    routeName: string;
    routeType: string;
    description?: string;
  }

  export interface UpdateRouteReq {
    stepIds: number[];
  }

  export interface RouteAddStepReq {
    stepOrder: number;
    stepCode: string;
    workstationCode: string;
    qualityCheckRequired: boolean;
    parameterDefinitions?: any;
  }

  export interface RouteRemoveStepReq {
    stepId: number;
  }

  export interface RouteReorderStepReq {
    stepIds: number[];
  }

  export interface RouteStepUpdateReq {
    stepId: number;
    executorEquipmentIds: number[];
  }

  export interface RouteAssignProcessesReq {
    processIds: number[];
  }

  export interface RouteProcessItem {
    processId: number;
    processCode: string;
    processName: string;
    stepOrder: number;
    remark?: string;
  }
}

/**
 * @name 路线管理模块
 */
// 获取路线列表
export const getRouteList = (params: any) => {
  return http.get<any>(`/routes/list`, params);
};

// 根据ID获取路线详情
export const getRouteById = (id: number) => {
  return http.get<Route.RouteListItem>(`/routes/${id}`);
};

// 根据状态获取路线列表
export const getRouteByStatus = (params: any, status: string) => {
  return http.get<any>(`/routes/status/${status}`, params);
};

// 创建路线
export const createRoute = (params: Route.CreateRouteReq) => {
  return http.post<number>(`/routes`, params);
};

// 更新路线
export const updateRoute = (id: number, params: Route.UpdateRouteReq) => {
  return http.put(`/routes/${id}`, params);
};

// 启用路线
export const enableRoute = (id: number) => {
  return http.post(`/routes/${id}/enable`);
};

// 禁用路线
export const disableRoute = (id: number) => {
  return http.post(`/routes/${id}/disable`);
};

// 发布路线
export const publishRoute = (id: number) => {
  return http.post(`/routes/${id}/publish`);
};

// 归档路线
export const archiveRoute = (id: number) => {
  return http.post(`/routes/${id}/archive`);
};

// 添加路线步骤
export const addRouteStep = (id: number, params: Route.RouteAddStepReq) => {
  return http.post(`/routes/${id}/steps`, params);
};

// 移除路线步骤
export const removeRouteStep = (id: number, params: Route.RouteRemoveStepReq) => {
  return http.delete(`/routes/${id}/steps`, params);
};

// 重新排序路线步骤
export const reorderRouteSteps = (id: number, params: Route.RouteReorderStepReq) => {
  return http.put(`/routes/${id}/steps/reorder`, params);
};

// 更新路线步骤
export const updateRouteStep = (id: number, params: Route.RouteStepUpdateReq) => {
  return http.put(`/routes/${id}/steps`, params);
};

// 删除路线
export const deleteRoute = (id: number) => {
  return http.delete(`/routes/${id}`);
};

// 批量删除路线
export const batchDeleteRoute = (ids: number[]) => {
  return http.post(`/routes/batch/delete`, { ids });
};

// 分配工序到工艺路线
export const assignProcessesToRoute = (routeId: number, params: Route.RouteAssignProcessesReq) => {
  return http.post(`/routes/${routeId}/steps`, params);
};

// 查询工艺路线的工序列表
export const getRouteProcesses = (routeId: number) => {
  return http.get<Route.RouteProcessItem[]>(`/routes/${routeId}/processes`);
};

/** 获取启用状态的工艺路线选项（用于下拉选择） */
export const getRouteOptions = () => {
  return http.get<{id: string, name: string}[]>("/routes/options");
};
